package mx.com.mindbits.bandpay.fragments;

import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.business.UserBusiness;
import mx.com.mindbits.bandpay.dao.UsersDAO;
import mx.com.mindbits.bandpay.utils.CardDialogHost;
import mx.com.mindbits.bandpay.utils.CardDialogHost.DialogListener;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.bandpay.utils.CustomToastNotification;
import mx.com.mindbits.bandpay.utils.GenericAlert;
import mx.com.mindbits.bandpay.utils.Utils;
import mx.com.mindbits.bandpay.vo.UserVO;
import mx.com.mindbits.dao.users;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

public class UserSettingsFragment extends SherlockFragment {
	private EditText userMail;
	private EditText passwordMail;
	private EditText link_privacity;
	private RadioGroup opc_smpt;
	
	private RadioButton otherSmpt;
	private RadioButton rbGmail;
	private RadioButton rbHotmail;
	private RadioButton rbYahoo;
	private Button clean;
	private Button save;
	private Context context;
	private UserVO _userVO;
	private String _smtp;
	private String _port;
	private UserBusiness _userBusiness;
	private UsersDAO userDAO;
	private users user;
	private boolean updateData = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		context = getActivity().getApplicationContext();
		_userBusiness = new UserBusiness(context);
		userDAO = new UsersDAO(context);
		user =new users();
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
		super.onCreateView(inflater, container, savedInstanceState);
		
		View view = inflater.inflate(R.layout.fragment_user_preference, container, false);
		context = view.getContext();//getActivity().getApplicationContext();
		save = (Button) view.findViewById(R.id.bt_acept);
		save.setEnabled(false);
		save.setBackgroundResource(R.drawable.button_disable_shape);
		userMail = (EditText) view.findViewById(R.id.et_user_mail);
		clean = (Button) view.findViewById(R.id.bt_clean);
		save.setEnabled(false);
		save.setBackgroundResource(R.drawable.button_disable_shape);
		
		passwordMail = (EditText) view.findViewById(R.id.et_user_pass);
		link_privacity = (EditText) view.findViewById(R.id.ed_link_privacity);
		otherSmpt = (RadioButton) view.findViewById(R.id.rb_otro);
		opc_smpt = (RadioGroup) view.findViewById(R.id.radioGroup1);
		rbGmail = (RadioButton) view.findViewById(R.id.rb_gmail);
		rbHotmail = (RadioButton) view.findViewById(R.id.rb_hotmail);
		rbYahoo = (RadioButton) view.findViewById(R.id.rb_yahoo);
		link_privacity.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if(arg0.length()>0){
					if(Utils.validaURL(link_privacity.getText().toString())){
						save.setEnabled(true);
						save.setBackgroundResource(R.drawable.blue_button_shape);
						clean.setEnabled(true);
						clean.setBackgroundResource(R.drawable.blue_button_shape);
					}else{
						save.setEnabled(false);
						save.setBackgroundResource(R.drawable.button_disable_shape);
						
					}
				}else{
					save.setEnabled(true);
					save.setBackgroundResource(R.drawable.blue_button_shape);
				}
				
			}
			
		});
		
		
		userMail.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable s) {}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				
				if(Utils.validaCorreo(userMail.getText().toString())){
					save.setEnabled(true);
					save.setBackgroundResource(R.drawable.blue_button_shape);
					clean.setEnabled(true);
					clean.setBackgroundResource(R.drawable.blue_button_shape);
				}
			}
			
		});
		user = userDAO.getLastUser();
		if(user !=null){
			Log.i(ConstantsValues.TAG_DAO, "" + user.getId_user());
			userMail.setText(user.getUser_email());
			passwordMail.setText(user.getUser_mail_pass());
			link_privacity.setText(user.getLink_privacity());
			_smtp = user.getHost();
			settingGroup(_smtp);
			_port = user.getPort();	
			updateData =true;
			save.setText("Actualizar");
		}else{
			_smtp = "";
			_port = "";
		}
		passwordMail.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {}
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {}
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				save.setEnabled(true);
				save.setBackgroundResource(R.drawable.blue_button_shape);
				clean.setEnabled(true);
				clean.setBackgroundResource(R.drawable.blue_button_shape);

			}
			
		});
		
		
		opc_smpt.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int id_radio) {
				selectSmpt(id_radio);
				save.setEnabled(true);
				save.setBackgroundResource(R.drawable.blue_button_shape);
				clean.setEnabled(true);
				clean.setBackgroundResource(R.drawable.blue_button_shape);
		
			}
			
		});
		
		otherSmpt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
					settingHostAndPortDialog();
				
			}
		});
		clean = (Button) view.findViewById(R.id.bt_clean);
		
		//save = (Button) view.findViewById(R.id.bt_acept);
		
		save.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				if(!Utils.validaCorreo(userMail.getText().toString())){
				 GenericAlert.generateAlertDialog(
						 context.getString(R.string.label_title_alertMail),
						 context.getString(R.string.label_msg_alertMail),
						 v.getContext()).show();
			 	}else{
			 		getPortHost();
			 		setUserVO();
			 		if(updateData){
			 			updateUser(v);
			 		}else{
			 			insertUserVO(v);
			 		}
			 		
			 	}
			}
		});
		
		clean.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				userMail.setText("");
				userMail.setHint(R.string.label_ejemplo_correo);
				passwordMail.setText("");
				link_privacity.setText("");
				_port = "";
				_smtp = "";
				save.setEnabled(false);
				save.setBackgroundResource(R.drawable.button_disable_shape);
				clean.setEnabled(false);
				clean.setBackgroundResource(R.drawable.button_disable_shape);
		
			}
		});
		return view;
	}
	
	public void setUserVO(){
		_userVO = new UserVO();
		_userVO.setEmail(userMail.getText().toString());
		_userVO.setPass_email(passwordMail.getText().toString());
		if(Utils.validaURL(link_privacity.getText().toString())){
			_userVO.setLink(link_privacity.getText().toString());
		}		
		Log.i(ConstantsValues.TAG_DAO, "set user VO" + _smtp);
		_userVO.setHost(_smtp);
		_userVO.setPort(_port);
	}
	
	public void insertUserVO(View v){
		try {
			_userBusiness.insertUser(_userVO);
			//Toast.makeText(v.getContext(),"Usuario insertado", Toast.LENGTH_SHORT).show();
			//Toast.makeText(v.getContext(),"Usuario"+_userVO.getHost(),Toast.LENGTH_SHORT ).show();
			CustomToastNotification.customMessage("Usuario insertado", context).show();
			save.setEnabled(false);
			save.setBackgroundResource(R.drawable.button_disable_shape);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void updateUser(View v){
		_userBusiness.updateUser(_userVO, user.getId_user());
		CustomToastNotification.customMessage("Usuario actualizado", context).show();
		save.setEnabled(false);
		save.setBackgroundResource(R.drawable.button_disable_shape);
	
	}
	
	public void selectSmpt(int id){
		switch(id){
		case R.id.rb_gmail:
			_smtp = ConstantsValues.SMTP_GMAIL;
			_port =  ConstantsValues.PORT_GMAIL;
			break;
		case R.id.rb_hotmail:
			_smtp = ConstantsValues.SMTP_HOTMAIL;
			_port = ConstantsValues.PORT_HOTMAIL;
			break;
		case R.id.rb_yahoo:
			_smtp = ConstantsValues.SMTP_YAHOO;
			_port = ConstantsValues.PORT_YAHOO;
			break;
		case R.id.rb_otro:
			//settingHostAndPortDialog();
			break;
		}
		
	}
	
	
	public void settingHostAndPortDialog(){
		CardDialogHost dialogHost = new CardDialogHost(context, _smtp, _port);
		dialogHost.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogHost.setDialogResult(new DialogListener(){

			@Override
			public void finish(String host, String port) {
				_smtp = host;
				_port = port;
			}
			
		});
		
		dialogHost.show();
	}
	
	public void settingGroup(String _smpt){
		Log.i(ConstantsValues.TAG_DAO, "" + _smpt);
		Log.i(ConstantsValues.TAG_DAO, "" + ConstantsValues.SMTP_GMAIL);
		if(_smpt.equals(ConstantsValues.SMTP_GMAIL)){
			rbGmail.setChecked(true);
		}else if(_smpt.equals(ConstantsValues.SMTP_HOTMAIL)){
			rbHotmail.setChecked(true);
		}else if(_smpt.equals(ConstantsValues.SMTP_YAHOO)){
			rbYahoo.setChecked(true);
		}else{
			Log.i(ConstantsValues.TAG_DAO, "" + opc_smpt.getChildCount());
			otherSmpt.setChecked(true);
			
		}
		
	}
	
	public void getPortHost(){
		
		if(rbGmail.isChecked()){
			_port = ConstantsValues.PORT_GMAIL;
			_smtp = ConstantsValues.SMTP_GMAIL;
		}else if(rbHotmail.isChecked()){
			_smtp = ConstantsValues.SMTP_HOTMAIL;
			_port = ConstantsValues.PORT_HOTMAIL;
		}else if(rbYahoo.isChecked()){
			_smtp = ConstantsValues.SMTP_YAHOO;
			_port = ConstantsValues.PORT_YAHOO;
		}else if(otherSmpt.isChecked()){
			//_smtp = ConstantsValues.SMTP_YAHOO;
			//_port = ConstantsValues.PORT_YAHOO;
		}
	}
		
	}
